﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using WinRT.SIP.UILogic.Repositories;

namespace WinRT.SIP.UILogic.ViewModels
{
    public class DialerPageViewModel : ViewModel
    {
        private string _phoneNumber;

        public string PhoneNumber
        {
            get { return _phoneNumber; }
            set { SetProperty(ref _phoneNumber, value); }
        }

        public DelegateCommand GoBackCommand { get; private set; }
        public DelegateCommand DialOneCommand { get; private set; }
        public DelegateCommand DialTwoCommand { get; private set; }
        public DelegateCommand DialThreeCommand { get; private set; }
        public DelegateCommand DialFourCommand { get; private set; }
        public DelegateCommand DialFiveCommand { get; private set; }
        public DelegateCommand DialSixCommand { get; private set; }
        public DelegateCommand DialSevenCommand { get; private set; }
        public DelegateCommand DialEightCommand { get; private set; }
        public DelegateCommand DialNineCommand { get; private set; }
        public DelegateCommand DialAsteriskCommand { get; private set; }
        public DelegateCommand DialZeroCommand { get; private set; }
        public DelegateCommand DialPoundCommand { get; private set; }
        public DelegateCommand CallCommand { get; private set; }
        public DelegateCommand DeleteCommand { get; private set; }

        public DialerPageViewModel(ICallRepository currentCallRepository, INavigationService navigationService)
        {
            GoBackCommand = new DelegateCommand(navigationService.GoBack, () => { return false; });

            DialOneCommand = new DelegateCommand(() => PhoneNumber += "1");
            DialTwoCommand = new DelegateCommand(() => PhoneNumber += "2");
            DialThreeCommand = new DelegateCommand(() => PhoneNumber += "3");
            DialFourCommand = new DelegateCommand(() => PhoneNumber += "4");
            DialFiveCommand = new DelegateCommand(() => PhoneNumber += "5");
            DialSixCommand = new DelegateCommand(() => PhoneNumber += "6");
            DialSevenCommand = new DelegateCommand(() => PhoneNumber += "7");
            DialEightCommand = new DelegateCommand(() => PhoneNumber += "8");
            DialNineCommand = new DelegateCommand(() => PhoneNumber += "9");
            DialAsteriskCommand = new DelegateCommand(() => PhoneNumber += "*");
            DialZeroCommand = new DelegateCommand(() => PhoneNumber += "0");
            DialPoundCommand = new DelegateCommand(() => PhoneNumber += "#");

            CallCommand = new DelegateCommand(() =>
                {
                    if (!String.IsNullOrEmpty(PhoneNumber))
                    {
                        currentCallRepository.SetCurrentCallee(PhoneNumber, false);
                        navigationService.Navigate("Call", null);
                    }
                });

            DeleteCommand = new DelegateCommand(() =>
                {
                    if (!String.IsNullOrEmpty(PhoneNumber))
                    {
                        PhoneNumber = PhoneNumber.Substring(0, PhoneNumber.Length - 1);
                    }
                });
        }
    }
}
